package neusoft.example.com.yuejian06.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.example.com.yuejian06.Bean.User;
import neusoft.example.com.yuejian06.Model.LoginModel;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

public class LoginActivity extends AppCompatActivity{
    private EditText user_tel,user_password;
    private Button login_button,login_register_button;
    private TextView forget_password;
    private LoginModel model;
    private Intent intent;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private RetrofitListener<User> listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        listener();

        initEvents();

    }

    private void initView(){
        user_tel = (EditText) findViewById(R.id.user_tel);
        user_password = (EditText) findViewById(R.id.user_password);
        login_button= (Button) findViewById(R.id.login_button);
        forget_password= (TextView) findViewById(R.id.forget_password);
        login_register_button = (Button) findViewById(R.id.login_register_button);
    }
    public void initEvents(){
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!user_tel.getText().toString().isEmpty()) {
                    model = new LoginModel();
                    model.login(user_tel.getText().toString(), user_password.getText().toString(),listener);
                }
            }
        });

        login_register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    public void listener(){
        listener=new RetrofitListener<User>() {
            @Override
            public void onResponse(User l) {
                if(!l.getUser_id().equals("0")) {
                    sp = getSharedPreferences("user_id", MODE_PRIVATE);
                    editor=sp.edit();
                    String user_id = l.getUser_id();
//                    editor.putString("user_tel", user_tel.getText().toString());
//                    editor.putString("user_password", user_password.getText().toString());
                    editor.putString("user_id", user_id);

                    editor.commit();
                    //saveUser(l.getUser_id());

                    intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
<<<<<<< HEAD
=======

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!user_tel.getText().toString().isEmpty()) {
                    model = new LoginModel();
                    model.login(user_tel.getText().toString(), user_password.getText().toString(),listener);
                }
            }
        });

        login_register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


    }

    private void initView(){
        user_tel = (EditText) findViewById(R.id.user_tel);
        user_password = (EditText) findViewById(R.id.user_password);
        login_button= (Button) findViewById(R.id.login_button);
        forget_password= (TextView) findViewById(R.id.forget_password);
        login_register_button = (Button) findViewById(R.id.login_register_button);
>>>>>>> 933bae1c0e2db0b5f4e6e201bd36bb77e66599e2
    }
}

